// Copyright 2012, Jun Chen .All rights reserved.
//
// Use of this source code is governed by a BSD-Style license that can be
// found in the License fule.
//
// Author: cpp.cheen at gmail dot com.


#ifndef IO_CHANNEL_H
#define IO_CHANNEL_H


#include    "pch_files.h"
#include    <boost/function.hpp>
#incluide   <vector>

class   evt_dispather;
class   io_buffer;
class io_channel:public boost::noncopyable
{
    public:
    typedef boost::function<void(x_int32)>          on_write_callback;
    typedef boost::function<void(io_buffer&)>       on_read_callback;
    typedef boost::function<void(x_int32&)>         on_error_callback;
    typedef boost::function<void()>                 on_close_callback;
    public:
        /** Default constructor */
        io_channel(evt_dispather&   evt_looper);
        /** Default destructor */
        virtual ~io_channel();
        x_int32&     evt_manage_flag();
        x_int32      evt_manage_flag()const;

        x_uint32&     evt_handle_flag();
        x_uint32      evt_handle_flag()const;

        x_fd         get_fd() const;

        void         set_read_call_back(on_read_callback fucntor);
        void         set_write_call_back(on_write_callback fucntor);
        void         set_error_call_back(on_error_callback fucntor);
        void         set_close_call_back(on_close_callback fucntor);

        void         handle_event();
    protected:

        /*
        void    on_send(x_int32&    bytes_trans);
        void    on_recv(io_buffer&    buffer);
        void    on_error(x_int32&    error_code);
        void    on_close();
        */

    private:
        /// io_channel do not close the fd
        x_fd        m_saved_fd;
        ///
        x_int32     m_evt_manage_flag;
        ///
        x_uint32    m_evt_handle_flag;
        ///
        evt_dispather&  m_evt_dispather;
        ///
        x_int64     m_bytes_wrote;
        ///
        x_int64     m_bytes_sendt;
        ///
        on_write_callback    on_send;
        ///
        on_read_callback    on_recv;
        ///
        on_error_callback   on_error;
        ///
        on_close_callback   on_close;
};

typedef std::vector<io_channel*>        io_channel_raw_ptr_vec;

#endif // IO_CHANNEL_H
